//
//  RadioViewController.m
//  Leisure
//
//  Created by Alex on 16/6/30.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "RadioViewController.h"

@interface RadioViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *scrollArray;
@property (nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation RadioViewController

- (NSMutableArray *)scrollArray {
    
    if (!_scrollArray) {
        _scrollArray = [NSMutableArray array];
    }
    return _scrollArray;
}

- (NSMutableArray *)dataSource {
    
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    // 数据请求
    [RequestManager fecthDataWithUrl:kRadioUrl parameter:@{} mehtod:@"POST" compelet:^(NSData *data) {
        
        NSDictionary *bigdic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *smallDic in bigdic[@"data"][@"carousel"]) {
            
            [self.scrollArray addObject:smallDic[@"img"]];
        }
        for (NSDictionary *smallDic in bigdic[@"data"][@"alllist"]) {
            
            RadioModel *model = [[RadioModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.dataSource addObject:model];
        }
 //       NSLog(@"%@",bigdic);
    } updateUI:^{
        [self.tableView reloadData];
        // 创建轮播图
        ZHScrollView *headerView = [[ZHScrollView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/3) imageArray:self.scrollArray];
     
        self.tableView.tableHeaderView = headerView;
        
 //       [self.tableView reloadData];
    }];
  
}

#pragma mark ---------TableViewDelegate----------

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.textLabel.text = [self.dataSource[indexPath.row] title];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RadioDetailViewController *radiodetail = [[RadioDetailViewController alloc]initWithNibName:@"RadioDetailViewController" bundle:nil];
    radiodetail.radioID = [self.dataSource[indexPath.row] radioid];
    
    [self.navigationController pushViewController:radiodetail animated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
   
}



@end
